import {Message} from "~/server/models/user";

// 返回所有用户
export default defineEventHandler(async (event) => {
    const method = event.node.req.method
    if (method === 'GET') {
        // 获取查询参数和分页参数
        const query = getQuery(event);
        const Q = {}
        const currentPage = parseInt(<string>query.currentPage || '1', 10); // 当前页，默认为第1页
        const PerPage = parseInt(<string>query.itemsPerPage || '10', 10); // 每页数量，默认为10

        // 计算跳过的记录数
        const skip = (currentPage - 1) * PerPage;

        // 执行分页查询
        const data = await Message.find(Q)
            .populate('user')
            .populate('to_user')
            .populate('group_name')
            .skip(skip)
            .limit(PerPage)

        // 可能还需要获取总记录数以计算总页数，这里简化处理未包含
        // const totalCount = await User.countDocuments(query);
        // const totalPages = Math.ceil(totalCount / itemsPerPage);

        return {
            query,
            currentPage,
            PerPage,
            data
        };
    } else {
        const body = await readBody(event)
        return {
            body,
        }
    }
})